﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq.Mapping;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(ICustomer))]
    public partial class Customer : Entity, ICustomer
    {

        #region ICustomer Members

        IQueryable<IAddress> _Addresses2;
        IQueryable<IAddress> ICustomer.Addresses
        {
            get {
                if (this._Addresses2 == null)
                {
                    this._Addresses2 = this.DataContext.Addresses
                        .Where(i => i.CustomerId == this.Id)
                        .Select(i => i as IAddress);
                }

                return this._Addresses2;
            }
        }



        IQueryable<IOrder> _Orders2;
        IQueryable<IOrder> ICustomer.Orders
        {
            get {
                if (this._Orders2 == null)
                {
                    this._Orders2 = this.DataContext.Orders
                        .Where(i => i.CustomerId == this.Id)
                        .Select(i => i as IOrder);
                }

                return this._Orders2;
            }
        }

        [AssociationAttribute(Name = "Address_Customer", Storage = "_Address", ThisKey = "DefaultAddressId", OtherKey = "Id", IsForeignKey = true)]
        [ColumnAttribute(IsDbGenerated = false, AutoSync = AutoSync.Never, UpdateCheck = UpdateCheck.Never)]
        public Address DefaultAddress
        {
            get;
            set;
        }
        
        IAddress _DefaultAddress;   
        IAddress ICustomer.DefaultAddress
        {
            get
            {
                if (this._DefaultAddress == null)
                {
                    this._DefaultAddress = this.DataContext.Addresses
                        .Where(i => i.Id == this.DefaultAddressId)
                        .FirstOrDefault();
                }

                return this._DefaultAddress;
            
            }
            set
            {
                this._DefaultAddress = value;
            }
        }

        IQueryable<ISupportTicket> _SupportTickets2;
        IQueryable<ISupportTicket> ICustomer.SupportTickets
        {
            get {
                if (this._SupportTickets2 == null)
                {
                    this._SupportTickets2 = this.DataContext.SupportTickets
                        .Where(i => i.CustomerId == this.Id)
                        .Select(i => i as ISupportTicket);
                }

                return this._SupportTickets2;
            }
        }



        ICountry country;
        ICountry ICustomer.Country
        {
            get
            {
                if (this.country == null && this.CountryCode != null)
                {
                    this.country = this.DataContext.Countries.Where(i => i.CountryCode == this.CountryCode)
                        .FirstOrDefault();
                }

                return this.country;
            }
            set
            {
                this.country = value;
                if (value == null)
                {
                    this.CountryCode = null;
                }
                else
                {
                    this.CountryCode = value.CountryCode;
                }
            }
        }

        #endregion
    }
}
